package com.wcgksoft.controller.controlEquipment;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.controlEquipment.ControlEquipment;
import com.wcgksoft.entity.dataAnalysis.DepartProjectData;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.valve.Valve;
import com.wcgksoft.service.DataAnalysis.DepartProjectDataService;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.controlEquipment.ControlEquipmentService;
import com.wcgksoft.service.valve.ValveService;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 *
 * 控制设备
 */
@Controller
@RequestMapping("system/control")
public class controlEquipmentController {


    @Autowired
    private ControlEquipmentService controlEquipmentService;

    @Autowired
    private EnterpriseProjectService enterpriseProjectService;

    @Autowired
    private ValveService valveService;

    @Autowired
    private DepartProjectDataService departProjectDataService;

    /**
     * 控制设备列表查询
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(
            @RequestParam(value = "areaName",required = false) String areaName,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit
    ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(ControlEquipment.class);
        //criteria.add(Restrictions.eq("departId", departId));
        if(StringUtils.isNotEmpty(areaName)){
            criteria.add(Restrictions.like("areaName", areaName, MatchMode.ANYWHERE));
        }
        List<ControlEquipment> list = controlEquipmentService.findByConditions(criteria,page,limit);
        for (ControlEquipment controlEquipment: list){
            controlEquipment.setArea(enterpriseProjectService.findById(controlEquipment.getAreaid()));
        }
        return Layui.data(0, "查询成功", controlEquipmentService.getTotalCount(), list);
    }

    /*
    * @Author: 周权
    * @Description:控制设备查询 无分页
    * @Param:
    * @create: 2020/3/26 17:09
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "findAlls")
    public Layui findAll() {
        DetachedCriteria criteria = DetachedCriteria.forClass(ControlEquipment.class);
        List<ControlEquipment> list = controlEquipmentService.findByConditions(criteria);
        return Layui.data(0, "查询成功", controlEquipmentService.getTotalCount(), list);
    }

    /**
     * 控制设备添加
     */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        Integer areaid = param.getInt("areaid");
        Integer valveid = param.getInt("controlVariable");
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            String[] dateFormats = new String[]{"yyyy-MM-dd"};
            JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));

            ControlEquipment controlEquipment = (ControlEquipment) JSONObject.toBean(JSONObject.fromObject(data), ControlEquipment.class);
            Valve byid =  valveService.findById(valveid);
            controlEquipment.setValve_name(byid.getValveName());
            controlEquipment.setValve_id(byid.getNumber());
            controlEquipment.setAreaName(enterpriseProjectService.findById(areaid).getProjectName());
            controlEquipmentService.saveOrUpdate(controlEquipment);
            return Layui.data(0, "添加成功", 1, Layui.addList(controlEquipment));
        }
    }


      /**
        *查询项目
        */
    @ResponseBody
    @RequestMapping(value = "getDepart")
    public List<EnterpriseProject> getDepart() {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("parentId", -1));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }

    /**
     * 查询片区
     */
    @ResponseBody
    @RequestMapping(value = "getArea")
    public List<EnterpriseProject> getArea(@RequestParam(value = "departid") Integer departid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("parentId", departid));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }

    /**
     * 查询关联变量
     */
    @ResponseBody
    @RequestMapping(value = "getData")
    public List<DepartProjectData> getData(@RequestParam(value = "areaid") Integer areaid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(DepartProjectData.class);
        criteria.add(Restrictions.eq("enterpriseProject.id", areaid));
        List<DepartProjectData> list = departProjectDataService.findByConditions(criteria);
        return list;
    }


  /**
   * 查询阀门表
   */
    @ResponseBody
    @RequestMapping(value = "findValve")
    public Layui findValve() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Valve.class);
        List<Valve> list = valveService.findByConditions(criteria);
        return Layui.data(0, "查询成功", valveService.getTotalCount(), list);
    }

    /**
     *
     * 控制设备删除
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        ControlEquipment byId = controlEquipmentService.findById(id);
        controlEquipmentService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }


}
